﻿Public Class LiteralTextInputViewModel
    Inherits RegexInputViewModelBase

#Region "Events"
    ' Insert Event Declarations Here
#End Region

#Region "Properties"
#Region "Constants"
    ' Insert Constant Declarations Here
#End Region

    Private WithEvents _input As ILiteralTextInput
    Protected Property Input As ILiteralTextInput
        Get
            Return _input
        End Get
        Private Set(value As ILiteralTextInput)
            _input = value
        End Set
    End Property

    Public Overrides ReadOnly Property HeaderText As String
        Get
            Return "Literal Text"
        End Get
    End Property

    Private Const PROPERTY_INPUT_TEXT As String = "InputText"
    Public Property InputText As String
        Get
            Return Me.Input.InputString
        End Get
        Set(value As String)
            Me.Input.InputString = value
        End Set
    End Property

    Private Const PROPERTY_REPLACEMENT_STRING As String = "ReplacementString"
    Public Property ReplacementString As String
        Get
            Return Me.Input.ReplacementString
        End Get
        Set(value As String)
            Me.Input.ReplacementString = value
        End Set
    End Property

    Private _replacementResult As String
    Public Property ReplacementResult() As String
        Get
            Return _replacementResult
        End Get
        Set(ByVal value As String)
            If _replacementResult <> value Then
                _replacementResult = value
                MyBase.OnPropertyChanged()
            End If
        End Set
    End Property

    Private Const PROPERTY_MATCHES As String = "Matches"
    Private _matches As MatchCollection
    Public Property Matches() As MatchCollection
        Get
            Return _matches
        End Get
        Private Set(ByVal value As MatchCollection)
            If _matches IsNot value Then
                _matches = value
                MyBase.OnPropertyChanged()
            End If
        End Set
    End Property

    Private _matchViewModels As RegexMatchesViewModel
    Public Property MatchViewModels() As RegexMatchesViewModel
        Get
            Return _matchViewModels
        End Get
        Set(ByVal value As RegexMatchesViewModel)
            If _matchViewModels IsNot value Then
                _matchViewModels = value
                MyBase.OnPropertyChanged()
            End If
        End Set
    End Property

    Private _selectedRegexItem As RegexViewModelBase
    Public Property SelectedRegexItem As RegexViewModelBase
        Get
            Return _selectedRegexItem
        End Get
        Set(value As RegexViewModelBase)
            If _selectedRegexItem IsNot value Then
                _selectedRegexItem = value
                MyBase.OnPropertyChanged()
            End If
        End Set
    End Property

    <PropertyDependency(PROPERTY_MATCHES)>
    Public ReadOnly Property MatchesFound As Boolean
        Get
            Return Me.Matches IsNot Nothing AndAlso Me.Matches.Count > 0
        End Get
    End Property
#End Region

#Region "Constructors"
    Public Sub New(input As ILiteralTextInput, parent As MainViewModel, viewNavigator As IViewNavigator)
        MyBase.New(parent, viewNavigator)

        Me.Input = input

        Me.AddCommand(
            "OpenFile",
            Sub(parameter As Object)
                Dim l_file = viewNavigator.GetFileHandle()
                If l_file IsNot Nothing Then
                    Using l_stream = l_file.OpenRead()
                        Dim l_reader = New IO.StreamReader(l_stream)
                        Dim l_contents = l_reader.ReadToEnd()
                        Me.InputText = l_contents
                        l_stream.Close()
                    End Using
                End If
            End Sub)

        Me.AddCommand(
            "Execute",
            Sub(parameter As Object)
                Dim l_regex = Me.Parent.CreateRegex("Literal Text - Matches")
                If l_regex IsNot Nothing Then
                    Dim l_matches = l_regex.Matches(Me.InputText)
                    Me.Matches = l_matches
                    Me.MatchViewModels = New RegexMatchesViewModel(Me.Matches, l_regex, viewNavigator)
                End If
            End Sub)

        Me.AddCommand(
            "Replace",
            Sub(parameter As Object)
                Dim l_regex = Me.Parent.CreateRegex("Literal Text - Replace")
                If l_regex IsNot Nothing Then
                    Dim l_replacementResult = l_regex.Replace(Me.InputText, Me.ReplacementString)
                    Me.ReplacementResult = l_replacementResult
                End If
            End Sub)
    End Sub
#End Region

#Region "Methods"
    ' Insert Method (Function/Sub) Declarations Here

#Region "Event Raiser Methods"
    ' Insert Method Declarations which Raise an Event Here
#End Region

#Region "Event Handlers"
    Private Sub LiteralTextInputViewModel_PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Handles Me.PropertyChanged
        Select Case e.PropertyName
            Case PROPERTY_INPUT_TEXT
                Me.Matches = Nothing
                Me.MatchViewModels = Nothing
                Me.SelectedRegexItem = Nothing
        End Select
    End Sub

    Private Sub Input_PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Handles _input.PropertyChanged
        Select Case e.PropertyName
            Case "InputString"
                OnPropertyChanged(PROPERTY_INPUT_TEXT)
        End Select
    End Sub
#End Region
#End Region

End Class
